package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.Prop;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.MallRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 显示微信配置
 * Created by zhangjh on 2016/12/4.
 */
@RestController
public class PanymentNotifyUrlController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(PanymentNotifyUrlController.class);

    public static final String sEncodingAESKey = "WtXLiyk28BaGC1ChARcpkgp0FIhqxFTCjNYLm86ucFL"; // 不需要修改
    public static final String sToken = "GWH2015GO"; // 不需要修改

    public static final String serverUrlPrefix = "http://api.wechat.rockcent.com/api/v2/wechat/config?domain=";
    @Autowired
    private BaseUserHelper userHelper;
    @Autowired
    private Prop prop;

    @Autowired
    MallRepository mallRepository;

    /**
     * @api {get} /mall/payment/notify/url/info 获取支付回调接口
     * @apiGroup wechat
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "sEncodingAESKey": "WtXLiyk28BaGC1ChARcpkgp0FIhqxFTCjNYLm86ucFL",
     *     "serverUrl": "http://api.wechat.rockcent.com/api/v2/wechat/config?domain=beta.c.dev.rockcent.com",
     *     "sToken": "GWH2015GO",
     *     "payAuthorizeUrl2": "http://beta.c.dev.rockcent.com/wechat/",
     *     "payAuthorizeUrl1": "http://beta.c.dev.rockcent.com/"
     *   }
     * }
     */
    @RequestMapping(value = Routes.PAYMENT_NOTIFY_URL_INFO, method = RequestMethod.GET)
    private JsonResult info(HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        Long mallId = userInfoDto.mall.getId();
        RestDoing doing = jsonResult -> {
            Mall mall = mallRepository.findOne(mallId);
//            log.info("======================>mall=====>" + mall);
            String domain = mall.getCustomerDomain() == null ? mall.getMobileHostName() : mall.getCustomerDomain();
            String serverUrl = serverUrlPrefix + domain;
            String payAuthorizeUrl1 = "http://" + domain + "/";
            String payAuthorizeUrl2 = "http://" + domain + "/wechat/";
            Map<String, String> resultMap = new HashMap<>();
//            String paymentNotifyUrlWechat = prop.paymentNotifyUrlWechat;
//            resultMap.put("paymentNotifyUrl", paymentNotifyUrlWechat);
            resultMap.put("serverUrl", serverUrl);
            resultMap.put("payAuthorizeUrl1", payAuthorizeUrl1);
            resultMap.put("payAuthorizeUrl2", payAuthorizeUrl2);
            resultMap.put("payAuthorizeUrl3", null);
            resultMap.put("sEncodingAESKey", sEncodingAESKey);
            resultMap.put("sToken", sToken);
            jsonResult.data = resultMap;
        };
        return doing.go(request, log);
    }


}
